/*
 Copyright (C) 2011 Brandon Chang (mingzhez@usc.edu)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CoreFoundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <CoreFoundation/CFLocale.h>
#import <sys/socket.h>
#import <sys/select.h>
#import "GrowlApplicationBridge.h"
#import "ethcard.h"
#import "userconfig.h"
#import "logs.h"
#import "des.h"
#import "md5.h"
#import "mytunet.h"
#import "mytunetsvc.h"
#import "setting.h"
#import "tunet.h"
#import "dot1x.h"
#import "util.h"
#import "os.h"
#import "UKLoginItemRegistry.h"

#define ACCOUNT1_NAME @"account1Name"
#define ACCOUNT2_NAME @"account2Name"
#define ACCOUNT1_PASSWORD @"account1Password"
#define ACCOUNT2_PASSWORD @"account2Password"
#define ACCOUNT1_ADAPTER @"account1Adapter"
#define ACCOUNT2_ADAPTER @"account2Adapter"
#define ACCOUNT1_LIMITATION @"account1Limitation"
#define ACCOUNT2_LIMITATION @"account2Limitation"
#define ACCOUNT1_USEDOT1X @"account1Usedot1x"
#define ACCOUNT2_USEDOT1X @"account2Usedot1x"
#define ACCOUNT_CHOICE @"accountChoice"
#define CHECK_PERMISSION @"check permission"
#define ENABLE_REMOTE_FUNCTION @"allowRemoteFunction"
#define ALLOW_REMOTE @"allowRemote"
#define REMOTE_PASSWORD @"remotePassword"
#define HIDE_LIMOTE @"hideLimoteWindow"
#define REMOTE_PORT_USER @"portNumber"

#define ALERT_STRING @"ALERT"
#define ALERT_TITLE_STRING @"ALERT TITLE"
#define QUIT_STRING @"QUIT"
#define DISCONNECTED_STRING @"DISCONNECTED"
#define CONNECTING_STRING @"CONNECTING"
#define CONNECTED_STRING @"CONNECTED"
#define DISCONNECTING_STRING @"DISCONNECTING"
#define CONNECT_STRING @"CONNECT"
#define DISCONNECT_STRING @"DISCONNECT"
#define INCOMPLETE_TITLE_STRING @"INCOMPLETE TITLE"
#define INCOMPLETE_STRING @"INCOMPLETE"
#define ACCOUNT_1_STRING @"ACCOUNT 1"
#define ACCOUNT_2_STRING @"ACCOUNT 2"
#define EMPTY_STRING @"EMPTY"
#define CONSOLE_STRING @"CONSOLE"
#define OPTIONS_STRING @"OPTIONS"
#define GROWL_CONNECTED_TITLE @"GROWL_CONNECTED_TITLE"
#define GROWL_CONNECTED_CONTENT @"GROWL_CONNECTED_CONTENT"
#define GROWL_DISCONNECTED_TITLE @"GROWL_DISCONNECTED_TITLE"
#define GROWL_DISCONNECTED_CONTENT @"GROWL_DISCONNECTED_CONTENT"
#define LOGON_ERROR @"LOGON_ERROR"
#define LOGON_ERROR_MSG @"LOGON_ERROR_MSG"
#define LOGON_ERROR_BUTTON @"LOGON_ERROR_BUTTON"
#define GROWL_ERROR_TITLE @"GROWL_ERROR_TITLE"
#define GROWL_ERROR_CONTENT @"GROWL_ERROR_CONTENT"
#define SAVE_PROMOT_TITLE @"SAVE_PROMOT_TITLE"
#define SAVE_PROMOT_BUTTON1 @"SAVE_PROMOT_BUTTON1"
#define SAVE_PROMOT_BUTTON2 @"SAVE_PROMOT_BUTTON2"
#define SAVE_PROMOT_CONTENT @"SAVE_PROMOT_CONTENT"
#define SAVE_PROMOT_CONTENT_ALERT @"SAVE_PROMOT_CONTENT_ALERT"
#define DELETE_CONFIRM_TITLE @"DELETE_CONFIRM_TITLE"
#define DELETE_CONFIRM_BUTTON1 @"DELETE_CONFIRM_BUTTON1"
#define DELETE_CONFIRM_BUTTON2 @"DELETE_CONFIRM_BUTTON2"
#define DELETE_CONFIRM_CONTENT @"DELETE_CONFIRM_CONTENT"
#define DONATE_CONFIRM_TITLE @"DONATE_CONFIRM_TITLE"
#define DONATE_CONFIRM_BUTTON1 @"DONATE_CONFIRM_BUTTON1"
#define DONATE_CONFIRM_BUTTON2 @"DONATE_CONFIRM_BUTTON2"
#define DONATE_CONFIRM_CONTENT @"DONATE_CONFIRM_CONTENT"
#define START_SERVICE @"START_SERVICE"
#define STOP_SERVICE @"STOP_SERVICE"
#define SERVICE_STOPPED @"SERVICE_STOPPED"
#define SERVICE_RUNNING @"SERVICE_RUNNING"
#define SEND_ERROR @"SEND_ERROR"
#define RECV_ERROR @"RECV_ERROR"
#define TIMEOUT @"TIMEOUT"
#define INVALID_PASSWORD @"INVALID_PASSWORD"
#define SIG_RECV @"SIG_RECV"
#define OK_BUTTON @"OK_BUTTON"
#define ACCOUNT1_TOOLTIP @"TOOLTIP_0"
#define ACCOUNT2_TOOLTIP @"TOOLTIP_1"
#define CONSOLE_TOOLTIP @"TOOLTIP_2"
#define OPTIONS_TOOLTIP @"TOOLTIP_3"

#define REMOTE_PORT 15698

@interface AppController : NSObject <GrowlApplicationBridgeDelegate>{
	IBOutlet NSPopUpButton *interfaces;
	IBOutlet NSPopUpButton *type;
	IBOutlet NSWindow *mainWindow;
	IBOutlet NSWindow *aboutWindow;
	IBOutlet NSTextField *usernameField;
	IBOutlet NSSecureTextField *passwordField;
	IBOutlet NSButton *useDot1x;
	NSThread *mainConnect;
	NSThread *remoteService;
	NSThread *remoteControl;
	IBOutlet NSMenu *statusMenu;
	NSStatusItem *statusItem;
	IBOutlet NSMenuItem *connectionStatus;
	IBOutlet NSMenuItem *connectItem;
	int statusTitle;
	NSString *money;
	NSImage *image_0;
	NSImage *image_1;
	NSImage *image_;
	BOOL quitChoice;
	NSString *alertContent;
	NSString *alertTitle;
	NSString *alertButton;
	NSString *connectString;
	NSString *connectingString;
	NSString *disconnectString;
	NSString *disconnectingString;
	NSString *connectedString;
	NSString *disconnectedString;
	NSImage *connectedImage;
	NSImage *disconnectedImage;
	NSImage *connectingImage;
	NSArray *networkInterfaces;
	IBOutlet NSMenuItem *account1;
	IBOutlet NSMenuItem *account2;
	IBOutlet NSView *accountView;
	IBOutlet NSView *terminalView;
	IBOutlet NSView *optionsView;
	BOOL connectJustPressed;
	IBOutlet NSButton *saveButton;
	IBOutlet NSButton *deleteButton;
	IBOutlet NSTextView *consoleContent;
	IBOutlet NSButton *clearButton;
	IBOutlet NSTextField *appName;
	IBOutlet NSTextField *appVersion;
	IBOutlet NSImageView *appIcon;
	IBOutlet NSTextView *appCredits;
	IBOutlet NSButton *autoStartButton;
	IBOutlet NSButton *autoStartButton_alt;
	IBOutlet NSButton *alowRemote;
	IBOutlet NSWindow *remoteSettingWindow;
	IBOutlet NSView *optionsView_alt;
	IBOutlet NSTextField *remotePasswordField;
	IBOutlet NSTextField *remoteControlPass;
	IBOutlet NSTextField *remoteServerIP;
	IBOutlet NSTextField *remoteIndicator;
	IBOutlet NSWindow *remoteControlWindow;
	IBOutlet NSButton *remoteGetStatusButton;
	IBOutlet NSButton *remoteConnectButton;
	IBOutlet NSButton *remoteDisconnectButton;
	IBOutlet NSButton *remoteSaveButton;
	IBOutlet NSButton *startServiceButton;
	IBOutlet NSButton *changeAccountButton;
	IBOutlet NSTextField *serviceIndicator;
	IBOutlet NSButton *remoteDoneButton;
	IBOutlet NSButton *remoteCancelButton;
	IBOutlet NSTextField *portNumberField;
	IBOutlet NSTextField *remotePortNumberField;
	IBOutlet NSWindow *limoteWindow;
	IBOutlet NSProgressIndicator *progressIndicator;
	IBOutlet NSWindow *gplWindow;
	NSLock *consoleLock;
	NSLock *flagLock;
	int selectedToolbarItem;
	NSString *emptyString;
	int consoleCount;
	int portNumber;
	int remotePortNumber;
	float maxScrollHeight;
	float currentPosition;
	BOOL restartAtTop;
	BOOL stopService;
	NSTimer *scrollTimer;
	NSTimeInterval startTime;
	NSCondition *condition;
}
- (void)sheetDidEnd:(NSWindow *)sheet
				returnCode:(int)returnCode
			   contextInfo:(void *)contextInfo;
-(IBAction)saveConfig:(id)sender;
-(IBAction)connect:(id)sender;
-(void)tryConnection;
-(void)connectionDidEnd:(NSNotification *)aNote;
-(IBAction)quitApp:(id)sender;
-(IBAction)showAccountWindow:(id)sender;
-(IBAction)showAboutWindow:(id)sender;
-(void)setStatus:(NSInteger)status;
-(void)setSaveButtonEnabled:(BOOL)enable;
-(IBAction)deviceChanged:(id)sender;
-(IBAction)typeChanged:(id)sender;
-(IBAction)portAuthorizationChanged:(id)sender;
-(void)setConnectItemEnabled:(NSNumber *)aNumber;
-(void)setAccountStatus:(NSNumber *)aNumber;
-(IBAction)chooseAccount:(id)sender;
-(void)switchToAccount1;
-(void)switchToAccount2;
-(void)switchToOptions;
-(void)cleanUpService;
-(IBAction)deleteAccount:(id)sender;
-(IBAction)showConsole:(id)sender;
-(IBAction)showOptions:(id)sender;
-(IBAction)clearConsole:(id)sender;
-(void)setConsoleString:(NSString *)aString;
-(void)growlAlert:(NSString *)aString title:(NSString *)aTitle;
-(IBAction)donate:(id)sender;
-(IBAction)closeAbout:(id)sender;
-(IBAction)goToWebsite:(id)sender;
-(IBAction)toggleAutoStart:(id)sender;
-(void)scrollCredits:(NSTimer *)timer;
-(IBAction)alowRemoteControl:(id)sender;
-(IBAction)remoteSave:(id)sender;
-(IBAction)remoteCancel:(id)sender;
-(IBAction)startStopService:(id)sender;
-(IBAction)remoteGetStatus:(id)sender;
-(IBAction)remoteConnect:(id)sender;
-(IBAction)remoteDisconnect:(id)sender;
-(IBAction)remoteChangeAccount:(id)sender;
-(IBAction)showRemoteController:(id)sender;
-(IBAction)closeRemoteController:(id)sender;
-(void)communicate:(NSNumber *)aNumber;
-(void)setEnableThreeButtons:(NSNumber *)boolKey;
-(IBAction)limoteDismiss:(id)sender;
-(IBAction)limoteCheckItOut:(id)sender;
-(IBAction)setShowLimoteWindow:(id)sender;
-(IBAction)showGPL:(id)sender;
-(IBAction)agreeGPL:(id)sender;
-(void)closeRemoteListenSocket:(int)listen_socket 
				  acceptSocket:(int)accept_socket
					  withPool:(NSAutoreleasePool *)pool;
-(void)closeCommunicateSocket:(int)socket
					 withPool:(NSAutoreleasePool *)pool;
//- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
@end
